// pages/clocklist/clocklist.js
import {Data} from './clocklist.data'
var app = getApp();
Page({
  data:Data,
  onLoad:function(options){
    var that = this
    app.getUserInfo(function(userInfo){
      that.setData({userInfo:userInfo})
    })
    getlist(that);
  },
  onShareAppMessage: function () {
    return {
      title: '爱丁生活方式管理营',
      path: '/pages/index/index'
    }
  },
  // onPullDownRefresh: function(){
  //   this.setData({
  //     offset:0,
  //     'clock.list':[]
  //   })
  //   var that = this;
  //   //这里有个 bug,不设置500ms 延迟可能数据没有重置
  //   setTimeout(function(){getlist(that,'pullDown')},500);
  // },
  navigateToClock: function(e) {
    toclock(e)
  },
  onReachBottom: function(){
    var that = this;
    getlist(that);
  }
})

// 查看打卡详情
function toclock(e) {
  var e_data = e.currentTarget.dataset;
  console.log(e);
  console.log(e_data)
  console.log(e_data.id)
  if( e_data.type ==8||e_data.type ==7||e_data.type==11){
    wx.navigateTo({
      url: '../chartlook/chartlook?dietId=' + e_data.id + '&type=' + e_data.type+"&times="+e_data.times +'&sign='+e_data.sign
    })
    return false;
  };

  if(e_data.type==9){
      wx.navigateTo({
        url: '../addcomplete/addcomplete?dietId=' + e_data.id + '&type=' + e_data.type + "&times=" + e_data.times+'&sign='+e_data.sign
      })
  }

  if (e_data.type != 6 && e_data.type != 9){
    wx.navigateTo({
      url: '../clock/clock?id=' + e_data.id + '&type=' + e_data.type + '&patientid=' + wx.getStorageSync('userInfo').patientid + "&times=" + e_data.times+'&sign='+e_data.sign
    })
  };
}
//获取打卡记录 
function getlist(that,pullDown) {
  if(that.data.hasMore) {
    var params = {
        'patientid': wx.getStorageSync('userInfo').patientid,
        'limit': that.data.limit,
        'offset': that.data.offset
    }
    app.https.get(that,'/IbabyWebService/camp/diehistory',params,function(res){
        var data = {};
        var list = that.data.clock.list;
        var clockList = that.data.clockList;
        console.log(res.list)
        for (var i = 0; i < res.list.length; i++) {
            res.list[i].detail = JSON.parse(res.list[i].detail);
      
            for (var j = 0; j < clockList[res.list[i].times - 1].datalist.length;j++){
              if (res.list[i].times == clockList[res.list[i].times - 1].datalist[j].times && res.list[i].type == clockList[res.list[i].times - 1].datalist[j].type ){
                 var  sign=j+1
              }
              res.list[i].sign=sign;
            }
            
        }
        
        res.list = list.concat(res.list);//数组合并
        data['clock'] = res;
        data['offset'] = that.data.limit + that.data.offset;
        if(that.data.offset>=0){
          that.setData({'hasMoreall':false})
        }
        if(res.total<=that.data.limit + that.data.offset) {
          that.setData({ 'hasMore': false, 'hasMoreall': true});
            setTimeout(function(){that.setData({'hasMore': true})},2000);
        }
        that.setData(data);
    },pullDown)
  }
}